#!/opt/homebrew/bin/python3.9

import abc
import math
import time


def sayHell():
    print("hello python")

sayHell()


class GameServer:
    def __init__(self, port) -> None:
        self.port = port
        self.running = False
    def start(self):
        self.running = True
    def stop(self):
        self.running = False
    def status(self):
        if self.running:
            print("server status:%d"%self.port)
        else:
            print("server stop")

gs = GameServer(8181)
gs.start()
gs.status()
gs.stop()
gs.status()

a,b,c = 1,2,3
if a > b:
    print("a > b")
elif a == b:
    print("a == b")
else:
    print("a < b")

for i in range(3):
    print("range:%d"%i)
i = 0
while i < 3:
    print("while i=%d"%i)
    i = i + 1

a1 = 12
a2 = 1.2
a3 = True
a4 = "aaa"
if a3:
    print("data=%d %f %s" % (a1,a2,a4))

alist = list()
alist.append(1)
alist.append(2)
alist.extend([33,44])
print(alist)

adict = dict()
adict["a"] = "a123"
print(adict,adict.get("a"))
for key in adict:
    print(key, adict[key])

aset = set()
aset.update({'x','y','z'})
print(aset)

astr = "abcdefg"
print(str(aset))
print(astr.upper())


# funcs
def add1(*args):
    sum = 0;
    for i in args:
        sum = sum + i
    return sum

print(add1(1,2,3,4,5,6))
def add2(**args):
    for key in args:
        print(key,args[key])
add2(name="123")

## exception
def try_err(n=0):
    try:
        if n == 0:
            raise SyntaxError("n is 0")
    except SyntaxError as e:
        print(e)
    finally:
        print("over")
try_err(0)

#三元
a = 1
print( -1 if a < 1 else 1)

#推导
a = [1, 2, 3, 4, 5]
print([i*i for i in a])

#断言
a = 11
assert(a > 10)

#with
with open("README.md", "r") as fp:
    contents = fp.readline()
    print(contents)

#lambda
f = lambda x,y: x+y
print(f(1,2))

# yield
def get_square(n):
    for i in range(n):
        yield(pow(i,2))
sq = get_square(10)
for i in sq:
    print(i, end=',')

#装饰器

def timer(func):
    def wrapper(*args, **kwds):
        t0 = time.time()
        func(*args, ** kwds)
        t1 = time.time()
        print("cost:%0.3f second"%(t1-t0))
    return wrapper

@timer
def do_something(delay):
    print("\nstart do something")
    time.sleep(delay)
    print("end do something")

do_something(1)

# 闭包
def log_factory(n):
    def log_n(x):
        return math.log(x)/math.log(n)
    return log_n
f1 = log_factory(3)
print(f1(13))


#类

class A:
    static_x = 10
    def __init__(self) -> None:
        self.instance_y = 111
    def __del__(self):
        print("end!!")
a = A()
print(A.static_x)
class Animal:
    def eat(self):
        print("eat something")
class Cat(Animal):
    def craw_tree(self):
        print("craw tree")
cat = Cat()
cat.eat()
cat.craw_tree()

class B1:

    def __init__(self, b, c) -> None:
        self.a = a
        self._b = b
        self.__c = c
    def getC(self):
        return self.__c
b1 = B1(11,22)
print(b1._b, b1.getC())
print(__name__)

# 抽象
class Ab(object, metaclass = abc.ABCMeta):
    @abc.abstractclassmethod
    def f1(self):
        pass
    @abc.abstractclassmethod
    def f2(self):
        pass
class Bb(Ab):
    def f1(self):
        print("f1 override")
    def f2(self):
        print("f2 override")
bb = Bb()
bb.f1()
bb.f2()
























